﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Table.Blo;
using System;

namespace UI
{
    public enum AutoChessWikiEntries
    {
        rule = 0, // 游戏规则
        round = 1, // 回合说明
        level = 2, // 等级经验
        coin = 3, // 金币说明
        hero = 4, // 英雄说明
        heroShop = 5 // 英雄商店
    }

    public class AutoChessWikiSubPage : SubContentBase
    {
        public Transform contentRoot;
        public ExplainPageLine paragraphTemplate;
        public Transform toggleGroupTransform;

        // Use this for initialization
        void Start()
        {

        }

        public override void OnShowTab()
        {
            base.OnShowTab();
            gameObject.SetActive(true);
            OpenTab(0);
        }

        public override void ReShowTab()
        {
            base.ReShowTab();
            this.gameObject.SetActive(true);
        }

        public override void CloseTab()
        {
            this.gameObject.SetActive(false);
            base.CloseTab();
        }

        public void OpenTab(int index)
        {
            if (index < 0 || index >= toggleGroupTransform.childCount)
            {
                ExLog.LogError("索引错误！");
                return;
            }

            if (contentRoot.childCount > 0)
            {
                contentRoot.DestroyChildren();
            }

            string textTableId = null;
            switch ((AutoChessWikiEntries)index)
            {
                default:
                case AutoChessWikiEntries.rule:
                    textTableId = "ACExplainPageWikiSubPageRuleTabContent";
                    break;
                case AutoChessWikiEntries.round:
                    textTableId = "ACExplainPageWikiSubPageRoundTabContent";
                    break;
                case AutoChessWikiEntries.level:
                    textTableId = "ACExplainPageWikiSubPageLevelTabContent";
                    break;
                case AutoChessWikiEntries.coin:
                    textTableId = "ACExplainPageWikiSubPageCoinTabContent";
                    break;
                case AutoChessWikiEntries.hero:
                    textTableId = "ACExplainPageWikiSubPageHeroTabContent";
                    break;
                case AutoChessWikiEntries.heroShop:
                    textTableId = "ACExplainPageWikiSubPageHeroShopTabContent";
                    break;
            }

            if (!string.IsNullOrEmpty(textTableId))
            {
                string content = TableTextBlo.GetText(textTableId);
                string[] txts = content.Split(new char[] { '*' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < txts.Length; i++)
                {
                    ExplainPageLine line = Instantiate(paragraphTemplate);
                    line.transform.SetParent(contentRoot.transform);
                    line.transform.localScale = Vector3.one;
                    line.SetGameObjectActiveEx(true);

                    var txt = txts[i];

                    if (txt.StartsWith("$"))
                    {
                        line.SetText(txt.Remove(0, 1));
                        line.SetHeaderIconActive(false);
                    }
                    else
                    {
                        line.SetText(txt);
                        line.SetHeaderIconActive(true);
                    }
                }
            }
        }
    }
}

